<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <link rel="shortcut icon" href="/favicon.ico">
    <title>重置密码</title>
    <link href="./dist/css/tabler.min.css?1674944402" rel="stylesheet"/>
    <link rel="stylesheet" href="./dist/css/jquery.toast.css">
</head>
<body class="d-flex flex-column">
    <div class="page page-center">
        <div class="container container-tight py-4">
            <div class="text-center mb-4">
                <img src="./image/bit-forum-logo01.png" height="50" alt="">
            </div>
            <div class="card card-md">
                <div class="card-body">
                    <h2 class="text-center mb-4">重置密码</h2>
                    <form id="resetForm" method="get" autocomplete="off" novalidate>
                        <div class="mb-3">
                            <label class="form-label required">邮箱</label>
                            <div class="input-group">
                                <input type="email" class="form-control" placeholder="请输入邮箱" id="email">
                                <button type="button" class="btn btn-secondary" id="sendCode">发送验证码</button>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label required">验证码</label>
                            <input type="text" class="form-control" placeholder="请输入验证码" id="code">
                        </div>
                        <div class="mb-3">
                            <label class="form-label required">新密码</label>
                            <input type="password" class="form-control" placeholder="请输入新密码" id="password">
                        </div>
                        <div class="mb-3">
                            <label class="form-label required">确认密码</label>
                            <input type="password" class="form-control" placeholder="请再次输入密码" id="passwordRepeat">
                        </div>
                        <div class="form-footer">
                            <button type="button" class="btn btn-primary w-100" id="submit">重置密码</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="text-center text-muted mt-3">
                想起密码了? <a href="./sign-in.html" tabindex="-1">返回登录</a>
            </div>
        </div>
    </div>
    <script src="./dist/js/jquery-3.6.3.min.js"></script>
    <script src="./dist/js/tabler.min.js"></script>
    <script src="./dist/js/jquery.toast.js"></script>
    <script>
        $(function() {
            let emailEl = $('#email');
            let codeEl = $('#code');
            let passwordEl = $('#password');
            let passwordRepeatEl = $('#passwordRepeat');
            let sendCodeBtn = $('#sendCode');
            
            // 发送验证码
            sendCodeBtn.click(function() {
                let email = emailEl.val();
                if (!email) {
                    $.toast({
                        heading: '警告',
                        text: '请输入邮箱',
                        icon: 'warning'
                    });
                    return;
                }
                
                // 发送请求获取验证码
                $.ajax({
                    type: 'post',
                    url: 'user/sendResetCode',
                    data: { email: email },
                    success: function(resp) {
                        if (resp.code == 0) {
                            $.toast({
                                heading: '成功',
                                text: '验证码已发送',
                                icon: 'success'
                            });
                            // 禁用按钮60秒
                            let seconds = 60;
                            sendCodeBtn.prop('disabled', true);
                            let timer = setInterval(function() {
                                sendCodeBtn.text(seconds + '秒后重试');
                                seconds--;
                                if (seconds < 0) {
                                    clearInterval(timer);
                                    sendCodeBtn.prop('disabled', false);
                                    sendCodeBtn.text('发送验证码');
                                }
                            }, 1000);
                        } else {
                            $.toast({
                                heading: '错误',
                                text: resp.message,
                                icon: 'error'
                            });
                        }
                    }
                });
            });
            
            // 提交重置密码
            $('#submit').click(function() {
                let email = emailEl.val();
                let code = codeEl.val();
                let password = passwordEl.val();
                let passwordRepeat = passwordRepeatEl.val();
                
                if (!email || !code || !password || !passwordRepeat) {
                    $.toast({
                        heading: '警告',
                        text: '请填写完整信息',
                        icon: 'warning'
                    });
                    return;
                }
                
                if (password !== passwordRepeat) {
                    $.toast({
                        heading: '警告',
                        text: '两次密码输入不一致',
                        icon: 'warning'
                    });
                    return;
                }
                
                $.ajax({
                    type: 'post',
                    url: 'user/resetPassword',
                    data: {
                        email: email,
                        code: code,
                        password: password,
                        passwordRepeat: passwordRepeat
                    },
                    success: function(resp) {
                        if (resp.code == 0) {
                            $.toast({
                                heading: '成功',
                                text: '密码重置成功,请重新登录',
                                icon: 'success',
                                afterHidden: function() {
                                    location.href = '/sign-in.html';
                                }
                            });
                        } else {
                            $.toast({
                                heading: '错误',
                                text: resp.message,
                                icon: 'error'
                            });
                        }
                    }
                });
            });
        });
    </script>
</body>
</html> 